* ==============================================================================
* ==============================================================================
* IMF Book: IMFBOOK_Ch7_Code.do
*			Chapter 7: The IMF and the Covid-19 response
* ==============================================================================
use "IMFBOOK_Ch7_Dataset.dta", clear
* ==============================================================================
* Generate variables
* ==============================================================================
encode cname, gen(cid)
xtset cid year
egen cm_ggx_ngdp_10_19 = mean(ggx_ngdp) if year>=2010 & year<=2019, by(cname)
egen m_hic_ggx_ngdp_10_19 = mean(ggx_ngdp) if year>=2010 & year<=2019 & HIC==1
egen m_mic_ggx_ngdp_10_19 = mean(ggx_ngdp) if year>=2010 & year<=2019 & MIC==1
egen m_lic_ggx_ngdp_10_19 = mean(ggx_ngdp) if year>=2010 & year<=2019 & LIC==1
gen dif_ggx_ngdp_23_10s = ggx_ngdp_Apr2022 - l4.cm_ggx_ngdp_10_19 if year==2023
gen L4cm_ggx_ngdp_10_19 = l4.cm_ggx_ngdp_10_19
foreach var of varlist ggx_ngdp* ggr_ngdp* {
	egen m_hic_`var' = mean(`var') if HIC==1, by(year)
	egen m_mic_`var' = mean(`var') if MIC==1, by(year)
	egen m_lic_`var' = mean(`var') if LIC==1, by(year)
}
gen debtservb_btot = (debtserv_ba + debtserv_bi)/1000000000
lab var debtservb_btot "PPG, bilateral debt service (billions USD)"
gen debtservb_mtot = (debtserv_ma + debtserv_mi)/1000000000
lab var debtservb_mtot "PPG, multilateral debt service (billions USD)"
gen debtservb_ptot = (debtserv_pa + debtserv_pi)/1000000000
lab var debtservb_ptot "PPG, private debt service (billions USD)"
gen debtservb_tot = debtserv_tot/1000000000
lab var debtservb_tot "PPG, total debt service (billions USD)"
gen dsrbtot_gdp = 100*debtservb_btot/ngdpd_Apr2022
replace dsrbtot_gdp = 100*debtservb_btot/ngdpd if year<2022
lab var dsrbtot_gdp "PPG, bilateral debt service (% of GDP)"
gen dsrmtot_gdp = 100*debtservb_mtot/ngdpd_Apr2022
replace dsrmtot_gdp = 100*debtservb_mtot/ngdpd if year<2022
lab var dsrmtot_gdp "PPG, multilateral debt service (% of GDP)"
gen dsrptot_gdp = 100*debtservb_ptot/ngdpd_Apr2022
replace dsrptot_gdp = 100*debtservb_ptot/ngdpd if year<2022
lab var dsrptot_gdp "PPG, private debt service (% of GDP)"
gen dsrtot_gdp = 100*debtservb_tot/ngdpd_Apr2022
replace dsrtot_gdp = 100*debtservb_tot/ngdpd if year<2022
lab var dsrtot_gdp "PPG, total debt service (% of GDP)"
gen ggr_usd = ngdpd_Apr2022 * (ggr_ngdp_Apr2022/100)
replace ggr_usd = ngdpd * (ggr_ngdp/100) if year<2022
lab var ggr_usd "General government revenue (billions USD)"
gen dsrbtot_rev = 100*debtservb_btot/ggr_usd
lab var dsrbtot_rev "PPG, bilateral debt service (% of revenues)"
gen dsrmtot_rev = 100*debtservb_mtot/ggr_usd
lab var dsrmtot_rev "PPG, multilateral debt service (% of revenues)"
gen dsrptot_rev = 100*debtservb_ptot/ggr_usd
lab var dsrptot_rev "PPG, private debt service (% of revenues)"
gen dsrtot_rev = 100*debtservb_tot/ggr_usd
lab var dsrtot_rev "PPG, total debt service (% of revenues)"
* ==============================================================================
* Factual statements
* ==============================================================================
xtset cid year
* "Indeed, 86 countries are expected to face fiscal contractions by 2023 (i.e., they will be spending less than their 2010s average), thereby exposing a cumulative total of 2.3 billion people to the socio-economic consequences of budget cuts."
count if dif_ggx_ngdp_23_10s>=0 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s>=-1 & dif_ggx_ngdp_23_10s<0 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s<-1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s<0 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
egen POP = total(lp) if f3.dif_ggx_ngdp_23_10s <0 & !missing(f3.dif_ggx_ngdp_23_10s)
sum POP
drop POP
* "First, high-income countries ... increased their year-on-year government spending by an unprecedented 7.3% of GDP. Per IMF projections, these expenditures were adjusted downwards from 2021 onwards, and by 2023 are expected to revert to their average for the 2010s, with further reductions projected for subsequent years."
list cname year m_hic_ggx_ngdp d.m_hic_ggx_ngdp m_hic_ggx_ngdp_Apr2022 d.m_hic_ggx_ngdp_Apr2022 m_hic_ggx_ngdp_10_19 if HIC==1
* "For middle income countries, we observe that the slow process of increasing public expenditures over the past decade will reverse. These countries increased government spending by approximately 2.9% of GDP in 2020 but have rapidly scaled back: by 2023, they will revert to their average for the 2010s, with further reductions projected for subsequent years."
list cname year m_mic_ggx_ngdp d.m_mic_ggx_ngdp m_mic_ggx_ngdp_Apr2022 d.m_mic_ggx_ngdp_Apr2022 m_mic_ggx_ngdp_10_19 if MIC==1
* "In contrast, low-income countries will experience a milder fiscal contraction, although their levels of government spending were very low to begin with and stagnant throughout the 2010s. The pandemic-related increase in government spending (1.2% of GDP) will much more gradually be phased out: by 2024, these countries are expected to revert to their average for the 2010s."
list cname year m_lic_ggx_ngdp d.m_lic_ggx_ngdp m_lic_ggx_ngdp_Apr2022 d.m_lic_ggx_ngdp_Apr2022 m_lic_ggx_ngdp_10_19 if LIC==1
* "For example, Jordan’s government spent on average 32.4% of GDP over the 2010s (close to the mean for MICs), while their spending is only expected to be 30.6% by 2023; we classify this 1.8-percentage point decrease as an instance of aggressive austerity."
sum L4cm_ggx_ngdp_10_19 if MIC==1
list cname L4cm_ggx_ngdp_10_19 ggx_ngdp_Apr2022 dif_ggx_ngdp_23_10s if year==2023 & cname=="Jordan"
* "In line with the evidence presented above, high-income countries are generally projected to be spending more by 2023 than they did over the 2010s:  half of these countries (32 out of 64) will have government expenditure that exceeds their pre-Covid average, nine will have moderate spending reductions, and the remaining 23 countries will experience aggressive austerity."
count if HIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s>=0 & HIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s>=-1 & dif_ggx_ngdp_23_10s<0 & HIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s<-1 & HIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if missing(dif_ggx_ngdp_23_10s) & HIC==1 & year==2023
* "Most low-income countries are also expected to expand: 17 will follow an expansionary path, one will experience moderate consolidation, and the remaining eight will pursue aggressive austerity. Almost all of the 9 contracting countries are in Africa (excluding Haiti, Tajikistan, and Yemen), and most are also classified by the World Bank as being in ‘fragile and conflict affected situations,’ which might also account for the fiscal problems they are facing."
count if LIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s>=0 & LIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s>=-1 & dif_ggx_ngdp_23_10s<0 & LIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s<-1 & LIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if missing(dif_ggx_ngdp_23_10s) & LIC==1 & year==2023
list cname ccode subregion_UN if dif_ggx_ngdp_23_10s<=0 & LIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
* "In contrast to the high prevalence of fiscal expansion among high- and low-income countries, data on the 99 middle-income countries show more diversity: 54 are expected to maintain an expansionary fiscal trajectory, 11 will experience a moderate contraction, and the remaining 34 countries will witness aggressive austerity. These headline figures mask important regional variation. For example, most South-East Asian middle-income countries are expected to implement extensive budget cuts. Similarly, over half of African middle-income countries—especially those in North and Central Africa—will face large-scale expenditure declines."
count if MIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s>=0 & MIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s>=-1 & dif_ggx_ngdp_23_10s<0 & MIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s<-1 & MIC==1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if missing(dif_ggx_ngdp_23_10s) & MIC==1 & year==2023
count if MIC==1 & subregion_UN=="South-eastern Asia" & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if MIC==1 & dif_ggx_ngdp_23_10s<-1 & subregion_UN=="South-eastern Asia" & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if MIC==1 & region_UN=="Africa" & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if MIC==1 & dif_ggx_ngdp_23_10s<-1 & region_UN=="Africa" & year==2023 & !missing(dif_ggx_ngdp_23_10s)
* "The heavy-contracting countries include some of the most populous nations, like Indonesia, Egypt, and Vietnam—each with a population of over 100 million"
list dif_ggx_ngdp_23_10s lp_Apr2022 if cname=="Indonesia" & year==2023
list dif_ggx_ngdp_23_10s lp_Apr2022 if cname=="Egypt" & year==2023
list dif_ggx_ngdp_23_10s lp_Apr2022 if cname=="Vietnam" & year==2023
* "For example, Indonesia had an average of government spending at 17.5% of GDP over the 2010s, already very low compared to the average of middle-income countries. Between 2019 and 2020, this jumped by 2.2 percentage points as the pandemic emerged; by 2023, the country's government expenditure is expected to be at 16.4%, or 1.1 percentage points lower than the 2010s average""
list cname year ggx_ngdp d.ggx_ngdp L4cm_ggx_ngdp_10_19 ggx_ngdp_Apr2022 dif_ggx_ngdp_23_10s if cname=="Indonesia"
* "government spending will decline in 86 countries, the majority of which are middle-income. In total, 65 countries will implement aggressive austerity measures, thereby impacting the lives of a cumulative total of 1.5 billion people"
count if dif_ggx_ngdp_23_10s<0 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
count if dif_ggx_ngdp_23_10s<-1 & year==2023 & !missing(dif_ggx_ngdp_23_10s)
egen POP = total(lp) if f3.dif_ggx_ngdp_23_10s<-1  & !missing(f3.dif_ggx_ngdp_23_10s)
sum POP
drop POP
* "The left panel presents average debt service in billions of US dollars. It shows the average middle-income country must pay $3.77bn on external debt service, $2.54bn (67%) of which is to the private sector—although these values are strongly influenced by large economies such as China ($44.60bn out of $49.50bn total debt service), Mexico ($27.83bn of $31.13bn), and Russia ($21.28bn out of $21.44bn)."
sum debtservb_tot debtservb_btot debtservb_mtot debtservb_ptot if year==2022 & LIC==0
sort debtservb_tot
list cname year debtservb_ptot debtservb_tot if year==2022 & LIC==0
* "The average low-income country pays $0.35bn in debt service, but only $0.07bn (20%) is to the private sector, with the bulk made up of bilateral and multilateral repayments."
sum debtservb_tot debtservb_btot debtservb_mtot debtservb_ptot if year==2022 & LIC==1
* "For the average middle-income country, debt service is 13.2% of revenues, of which 4.7% of revenues is to the private sector; for the average low-income country it is 11.6% and 1.3% of revenues respectively."
sum dsrtot_rev dsrbtot_rev dsrmtot_rev dsrptot_rev if year==2022 & LIC==0
sum dsrtot_rev dsrbtot_rev dsrmtot_rev dsrptot_rev if year==2022 & LIC==1
* "In particular, middle-income countries spent 2.8% of GDP on health in 2019, compared with an expected 3.2% of GDP on external debt service in 2022; for low-income countries, these figures are 1.1% and 1.9% of GDP respectively."
sum dsrtot_gdp dsrbtot_gdp dsrmtot_gdp dsrptot_gdp if year==2022 & LIC==0
sum dsrtot_gdp dsrbtot_gdp dsrmtot_gdp dsrptot_gdp if year==2022 & LIC==1
* Of the countries identified, ten are projected by the IMF to continue fiscal expansion up to 2023, ten will experience austerity, and three countries (Afghanistan, Ecuador, and Ukraine) have no data available.
preserve
import excel "IMFBOOK_Ch7_CovidLendingData.xlsx", sheet("Data") firstrow case(lower) clear
gen type2 = typeof
keep if type2 == "Extended Credit Facility (ECF)" | type2 == "Extended Fund Facility (EFF)" | type2 == "Stand-By Arrangement (SBA)" | type2 == "Standby Credit Facility (SCF)"
list country
egen x = group(country)
sum x
restore
* ==============================================================================
* Figure 7.1. Government expenditure in high-, middle- and low-income countries
* ==============================================================================
twoway (scatter m_hic_ggx_ngdp year if year>=2010 & year<=2021, sort(year) connect(l) msymbol(i) lwidth(thick) lcolor(black))  (lfit m_hic_ggx_ngdp year if year>=2010 & year<=2019, sort(year) connect(l) msymbol(i) lpattern(dash) lcolor(black)) (scatter m_hic_ggx_ngdp_Apr2022 year if year>=2019 & year<=2027, sort(year) connect(l) msymbol(square) mcolor(black) lcolor(black)) (scatter m_mic_ggx_ngdp year if year>=2010 & year<=2021, sort(year) connect(l) msymbol(i) lwidth(thick) lcolor(gs8))  (lfit m_mic_ggx_ngdp year if year>=2010 & year<=2019, sort(year) connect(l) msymbol(i) lpattern(dash) lcolor(gs8)) (scatter m_mic_ggx_ngdp_Apr2022 year if year>=2019 & year<=2027, sort(year) connect(l) msymbol(square) mcolor(gs8) lcolor(gs8))  (scatter m_lic_ggx_ngdp year if year>=2010 & year<=2021, sort(year) connect(l) msymbol(i) lwidth(thick) lcolor(gs12)) (lfit m_lic_ggx_ngdp year if year>=2010 & year<=2019, sort(year) connect(l) msymbol(i) lpattern(dash) lcolor(gs12)) (scatter m_lic_ggx_ngdp_Apr2022 year if year>=2019 & year<=2027, sort(year) connect(l) msymbol(square) mcolor(gs12) lcolor(gs12)) , graphregion(color(white)) legend(row(2)) ytitle(Government expenditure (% of GDP)) ylabel(, angle(horizontal)) xlabel(2010(1)2027, angle(45)) title("") legend(size(small) rows(3) label(1 "HIC spending") label(2 "HIC trend 2010-19") label(3 "HIC projection") label(4 "MIC spending") label(5 "MIC trend 2010-19") label(6 "MIC projection") label(7 "LIC spending") label(8 "LIC trend 2010-19") label(9 "LIC projection"))
graph export "Figures\IMFBOOK_Figure7-1.png", replace width(4000)
* ==============================================================================
* Figure 7.2. Government expenditure changes as a share of GDP, 2010s vs 2023
* ==============================================================================
preserve
*** get the shapefiles in order
capture cd "GIS"
spshape2dta "World_Countries__Generalized_.shp", replace saving(world)
*** explore the outline file
use world_shp, clear
geo2xy _Y _X, proj (web_mercator) replace
*** merge with attributes file to get the names
merge m:1 _ID using world
drop if COUNTRY=="Antarctica"
drop rec_header- _merge
sort _ID
save, replace
*** merging
use world, clear
ren COUNTRY cname             
drop if cname=="Antarctica"
replace cname="Democratic Republic of the Congo" if cname=="Congo DRC"
replace cname="Hong Kong SAR" if cname==""
replace cname="Islamic Republic of Iran" if cname=="Iran"
replace cname="Korea" if cname=="South Korea"
replace cname="Kosovo" if cname==""
replace cname="Kyrgyz Republic" if cname=="Kyrgyzstan"
replace cname="Lao P.D.R." if cname=="Laos"
replace cname="Macao SAR" if cname==""
replace cname="Republic of Congo" if cname=="Congo"
replace cname="Russia" if cname=="Russian Federation"
replace cname="Slovak Republic" if cname=="Slovakia"
replace cname="St. Kitts and Nevis" if cname=="Saint Kitts and Nevis"
replace cname="St. Lucia" if cname=="Saint Lucia"
replace cname="St. Vincent and the Grenadines" if cname=="Saint Vincent and the Grenadines"
replace cname="São Tomé and Príncipe" if cname=="Sao Tome and Principe"
replace cname="Taiwan Province of China" if cname==""
replace cname="The Bahamas" if cname=="Bahamas"
replace cname="The Gambia" if cname=="Gambia"
replace cname="West Bank and Gaza" if cname=="Palestinian Territory"
merge 1:m cname using "..\IMFBOOK_Ch7_Dataset.dta"
*** see which countries didn't merge
tab cname if _m==1 
tab cname if _m==2 
encode cname, gen(cid)
xtset cid year
egen cm_ggx_ngdp_10_19 = mean(ggx_ngdp) if year>=2010 & year<=2019, by(cname)
gen dif_ggx_ngdp_23_10s = ggx_ngdp_Apr2022 - l4.cm_ggx_ngdp_10_19 if year==2023
spmap dif_ggx_ngdp_23_10s using world_shp if year==2023, id(_ID) ocolor(black ..) osize(vvthin ..) fcolor(black gs8 gs12) legend(pos(7) size(*1)) legtitle("Percentage point change in government" "spending (% of GDP), 2010s vs 2023") legstyle(2) legend(margin(vsmall) label(1 "Missing") label(2 "Reduction over 1 p.p." "(agressive austerity)") label(3 "Reduction between 0 and 1 p.p." "(moderate contraction)") label(4 "Increase or no change" "(expansionary fine-tuning")) ndfcolor(white ..) ndlabel("Missing") ndsize(vvthin ..) clmethod(custom) clbreaks(-45 -1 0 30)
graph export "..\Figures\IMFBOOK_Figure7-2.png", replace width(4000)
capture cd ..
restore
* ==============================================================================
* Figure 7.3. External debt service in middle- and low-income countries for 2022
* ==============================================================================
graph bar (mean) debtservb_tot debtservb_btot debtservb_mtot debtservb_ptot if year==2022, over(LIC, relabel (1 "MICs" 2 "LICs")) graphregion(color(white)) legend(lab(1 Total) lab(2 Bilateral) lab(3 Multilateral) lab(4 Private)) ylabel(,angle(horizontal)) ytitle("Average external debt service (billions of USD)") name(g1) scheme(s2mono)
graph bar (mean) dsrtot_rev dsrbtot_rev dsrmtot_rev dsrptot_rev if year==2022, over(LIC, relabel (1 "MICs" 2 "LICs")) graphregion(color(white)) legend(lab(1 Total) lab(2 Bilateral) lab(3 Multilateral) lab(4 Private)) ylabel(,angle(horizontal)) ytitle("Average external debt7 service (% of revenues)") name(g2) scheme(s2mono)
grc1leg g1 g2, graphregion(color(white))
graph export "Figures\IMFBOOK_Figure7-3.png", replace width(4000)
graph drop _all
* ==============================================================================
* Table 7.1. IMF approved funding by facility (2 March 2020 to 1 March 2022)
* ==============================================================================
preserve
import excel "IMFBOOK_Ch7_CovidLendingData.xlsx", sheet("Data") firstrow case(lower) clear
gen type2 = typeof
replace type2 = "Extended Credit Facility (ECF)" if typeof=="Augmentation of ECF"
replace type2 = "Extended Fund Facility (EFF)" if typeof=="Augmentation of EFF"
replace type2 = "Flexible Credit Line (FCL)" if typeof=="Augmentation of FCL"
replace type2 = "Stand-By Arrangement (SBA)" if typeof=="Augmentation of SBA"
replace type2 = "Standby Credit Facility (SCF)" if typeof=="Augmentation of SCF"
tabstat amountapprovedinusdmillion, by(type2) s(n sum) labelwidth(32)
by type2 country, sort: gen nvals = _n==1
by type2: replace nvals = sum(nvals)
tabstat nval, by(type2) s(max) nototal
egen x = group(country)
sum x
restore
* ==============================================================================
